package org.jeecg.modules.phone.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.common.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.phone.entity.*;
import org.jeecg.modules.phone.enums.EntityEnums;
import org.jeecg.modules.phone.enums.ResourcesEnums;
import org.jeecg.modules.phone.enums.TaskEnums;
import org.jeecg.modules.phone.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.phone.service.impl.PhoneEntityServiceImpl;
import org.jeecg.modules.system.util.ResultUtils;
import org.jeecg.modules.system.util.sysUtils;
import org.jeecg.modules.system.vo.ResultVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 资源管理
 * @Author: jeecg-boot
 * @Date:   2022-08-31
 * @Version: V1.0
 */
@Slf4j
@Api(tags="资源管理")
@RestController
@RequestMapping("/phone/phoneEntityTaskRsource")
public class PhoneEntityTaskRsourceController extends JeecgController<PhoneEntityTaskRsource, IPhoneEntityTaskRsourceService> {
	@Autowired
	private IPhoneEntityTaskRsourceService phoneEntityTaskRsourceService;

	 @Autowired
	 private IPhoneTaskService phoneTaskService;
	 @Autowired
	 private IPhoneTaskChildrenService phoneTaskChildrenService;

	 @Autowired
	 private  IPhoneEntityService phoneEntityService;

	 @Autowired
	 private IPhoneEntityTaskService phoneEntityTaskService;
	/**
	 * 分页列表查询
	 *
	 * @param phoneEntityTaskRsource
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "资源管理-分页列表查询")
	@ApiOperation(value="资源管理-分页列表查询", notes="资源管理-分页列表查询")
	@GetMapping(value = "/list")
	public JSONObject queryPageList(PhoneEntityTaskRsourceVo phoneEntityTaskRsource,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		QueryWrapper<PhoneEntityTaskRsourceVo> queryWrapper = QueryGenerator.initQueryWrapper(phoneEntityTaskRsource, req.getParameterMap());
		Page<PhoneEntityTaskRsourceVo> page = new Page<PhoneEntityTaskRsourceVo>(pageNo, pageSize);

		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = loginUser.getId();
		String username = loginUser.getUsername();
		queryWrapper.eq( "e.create_by", username );
		queryWrapper.orderByDesc( "e.create_time");
		IPage<PhoneEntityTaskRsourceVo> pageList = phoneEntityTaskRsourceService.pageList(page, queryWrapper);
		return ResultUtils.ResultOkFromPage(pageList);
	}

	/**
	 * 添加
	 *
	 * @param phoneEntityTaskRsource
	 * @return
	 */
	@AutoLog(value = "资源管理-添加")
	@ApiOperation(value="资源管理-添加", notes="资源管理-添加")
	@PostMapping(value = "/add")
	public ResultVo<?> add(@RequestBody PhoneEntityTaskRsource phoneEntityTaskRsource) {
		phoneEntityTaskRsource.setResourceStatus("0");
		phoneEntityTaskRsourceService.save(phoneEntityTaskRsource);
//		if(phoneEntityTaskRsource.getResourceStatus().equals("1")){
//			getChildrenTaskRun(phoneEntityTaskRsource);
//		}

		return ResultUtils.OK("添加成功！");
	}





//	/**
//	 * 编辑
//	 *
//	 * @param phoneEntityTaskRsource
//	 * @return
//	 */
//	@AutoLog(value = "资源管理-编辑")
//	@ApiOperation(value="资源管理-编辑", notes="资源管理-编辑")
//	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<?> edit(@RequestBody PhoneEntityTaskRsource phoneEntityTaskRsource) {
//		phoneEntityTaskRsourceService.updateById(phoneEntityTaskRsource);
//		return Result.OK("编辑成功!");
//	}


	 /**
	  * 下拉框数据
	  *

	  */
	 @AutoLog(value = "资源管理-下拉框数据")
	 @ApiOperation(value="资源管理-下拉框数据", notes="资源管理-下拉框数据")
	 @RequestMapping(value = "/downList", method = {RequestMethod.PUT,RequestMethod.POST})
	 public ResultVo<?> downList(@RequestBody PhoneEntity phoneEntity ) {
//		 先看这个远程机的手机列表。和任务列表。
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String userId = loginUser.getId();
		 String username = loginUser.getUsername();

		 List<PhoneEntity> phoneEntities = phoneEntityService.list(
				 new QueryWrapper<PhoneEntity>().lambda()
						 .eq(PhoneEntity::getCreateBy, username )
		 );

		 List<PhoneTask> phoneTasks = phoneTaskService.list();
		 JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
		 jsonObject.put("entityList", phoneEntities);
		 jsonObject.put("taskList", phoneTasks );
		 return ResultUtils.OK(jsonObject);
	 }



	 @AutoLog(value = "资源管理-执行开始")
	 @ApiOperation(value="资源管理-执行开始", notes="资源管理-执行开始")
	 @RequestMapping(value = "/taskAdd", method = {RequestMethod.PUT,RequestMethod.POST})
	 public ResultVo<?> taskAdd( @RequestBody PhoneEntityTaskRsource phoneEntityTaskRsource ) {
//		 phoneEntityTaskRsource.setResourceStatus(ResourcesEnums.ResourcesStatus.ENABLED.getName());
//		 phoneEntityTaskRsourceService.updateById(phoneEntityTaskRsource);
//		 getChildrenTaskRun(phoneEntityTaskRsource);
//		 return ResultUtils.OK("任务添加完成");
//		 以前这个接口就是开始任务了。现在1012后，这个是切换是否同步

		 /*同步的逻辑是，一个手机只能存在一个同步任务。 */
		 /*传参必 id,  entityId */
		 if(StringUtils.isNullOrEmpty(phoneEntityTaskRsource.getId())){
			 return ResultUtils.OK("缺少id");
		 }
		 if(StringUtils.isNullOrEmpty(phoneEntityTaskRsource.getEntityId())){
			 return ResultUtils.OK("缺少EntityId");
		 }
		 phoneEntityTaskRsourceService.resetStatus(phoneEntityTaskRsource.getEntityId());
		 phoneEntityTaskRsource.setResourceStatus(ResourcesEnums.ResourcesStatus.ENABLED.getName());
		 phoneEntityTaskRsourceService.updateById(phoneEntityTaskRsource);






		 return ResultUtils.OK("同步成功");
	 }




	 @AutoLog(value = "资源管理-测试")
	 @ApiOperation(value="资源管理-测试", notes="资源管理-测试")
	 @RequestMapping(value = "/taskAddTest", method = {RequestMethod.PUT,RequestMethod.POST})
	 public ResultVo<?> taskAddTest( @RequestBody PhoneEntityTaskRsource phoneEntityTaskRsource ) {
		 PhoneEntityTaskRsource byId = phoneEntityTaskRsourceService.getById("1564913572773363713");




		 return ResultUtils.OK("任务添加完成");
	 }






	/**
	 * 通过id删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "资源管理-通过id删除")
	@ApiOperation(value="资源管理-通过id删除", notes="资源管理-通过id删除")
	@PostMapping(value = "/delete")
	public ResultVo<?> delete( @RequestBody IdsVo ids ) {
//		不能等于运行中

		PhoneEntityTaskRsource byId = phoneEntityTaskRsourceService.getById(ids.getId());
		PhoneEntity phoneEntity = phoneEntityService.getById(byId.getEntityId());
		if( phoneEntity.getEntityStatus().equals(EntityEnums.EntityStatus.RUNNING.getName()) ){
			return ResultVo.error("该手机执行任务中，需停止任务再尝试删除。");
		}



		phoneEntityTaskRsourceService.removeById(ids.getId());
		return ResultVo.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "资源管理-批量删除")
	@ApiOperation(value="资源管理-批量删除", notes="资源管理-批量删除")
	@PostMapping(value = "/deleteBatch")
	public ResultVo<?> deleteBatch(@RequestBody IdsVo ids ) {

		if(  phoneEntityTaskRsourceService.checkRunning(Arrays.asList(ids.getIds().split(","))) ){
			return ResultVo.error("该手机执行任务中，需停止任务再尝试删除。");
		}
		this.phoneEntityTaskRsourceService.removeByIds(Arrays.asList(ids.getIds().split(",")));
		return ResultVo.OK("批量删除成功！");
	}
//
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "资源管理-通过id查询")
//	@ApiOperation(value="资源管理-通过id查询", notes="资源管理-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
//		PhoneEntityTaskRsource phoneEntityTaskRsource = phoneEntityTaskRsourceService.getById(id);
//		return Result.OK(phoneEntityTaskRsource);
//	}
//
//  /**
//   * 导出excel
//   *
//   * @param request
//   * @param phoneEntityTaskRsource
//   */
//  @RequestMapping(value = "/exportXls")
//  public ModelAndView exportXls(HttpServletRequest request, PhoneEntityTaskRsource phoneEntityTaskRsource) {
//      return super.exportXls(request, phoneEntityTaskRsource, PhoneEntityTaskRsource.class, "资源管理");
//  }
//
//  /**
//   * 通过excel导入数据
//   *
//   * @param request
//   * @param response
//   * @return
//   */
//  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//      return super.importExcel(request, response, PhoneEntityTaskRsource.class);
//  }

}
